<?php

/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2015/5/11
 * Time: 14:35
 */
class GetInfoAction extends Action
{
    public function index(){
            $url = "http://".$_SERVER{'HTTP_HOST'}.'/authorize.php';
            redirect('https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx7a4c3c0122f36dad&redirect_uri=' . $url.'&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect');
            $this->display();
    }

    public function httpGet($url)
    {
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        return $output;
    }

    public function getInfo(){
        import('ORG.Util.Session');

        $user['openid'] = $_GET['openid'];
        $user['nickname'] = $_GET['nickname'];
        $user['imgurl'] = $_GET['headimgurl'];
        $user['sex'] = $_GET['sex'];
        $user['province'] = $_GET['province'];
        $user['city'] = $_GET['city'];
        $user['country'] = $_GET['country'];
        $user['createtime'] = time();
        if ($user['openid']) {
            $exist = M('wxuser')->where(array('openid'=>$user['openid']))->find();
            if(!$exist){
                M('wxuser')->add($user);
            }
            session::set('openid',$user['openid']);
            $action = session::get('action');
            $func = session::get('func');
            session::set('action');
            session::set('func');
            $this->redirect($action.'/'.$func);
            
            
        } else {
            echo "<SCRIPT language=JavaScript>alert('没有获取到用户信息！');location.href='javascript:history.go(-1);';</SCRIPT>";
            echo ' ';
            exit;
        }

    }

    
}